//
//  LDArtifactTabController.m
//  Gift
//
//  Created by 李迪 on 15/11/17.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDArtifactTabController.h"
#import "LDArtifactCell.h"
#import "LDArtifactItem.h"
#import "BaseHeader.h"
#import "LDHadelItem.h"
#import "LDArtifactModel.h"
#import "LDArtifactGiftController.h"
#import "LDArtifactController.h"

@interface LDArtifactTabController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain)UITableView *tab;
@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *arrayTow;

@end

@implementation LDArtifactTabController

-(void)dealloc{
    
    self.tab.delegate = nil;
    self.tab.dataSource = nil;
    self.collection.dataSource = nil;
    self.collection.delegate = nil;
    [self.tab release];
    [self.collection release];
    [super dealloc];
}

#pragma mark - 数据申请
- (void)handleData{
    
    [PFAFNetworking getUrl:@"http://api.liwushuo.com/v2/item_categories/tree" body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        self.array = [[NSMutableArray array]init];
        self.arrayTow = [[NSMutableArray array]init];
        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"categories"]) {
            LDArtifactModel *model = [LDArtifactModel modelSteDic:dic];
            [self.array addObject:model];
            for (NSDictionary *dicTow in [dic objectForKey:@"subcategories"]) {
                LDArtifactModel *modelTow = [LDArtifactModel modelSteDic:dicTow];
                [self.arrayTow addObject:modelTow];

            }
        }
        [self.tab reloadData];
        [self createTabView];
        [self createCollection];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
       
    }];
    
    
}


#pragma mark - 创建tab对象
- (void)createTabView{
    
    self.tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 80, SCREEN_HEIGHT - 113) style:UITableViewStylePlain];
    [self.tab registerClass:[LDArtifactCell class] forCellReuseIdentifier:@"LDArtifactCell"];
    self.tab.delegate = self;
    self.tab.dataSource = self;
    [self.view addSubview:self.tab];
    self.tab.showsVerticalScrollIndicator = NO;
    self.tab.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tab release];
}

#pragma mark - 创建瀑布流对象
- (void)createCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    //最小间距设置
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 30;
    layout.itemSize = CGSizeMake(60, 60);
    //
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 10);
    
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
    
    //创建瀑布流
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(80, 0, SCREEN_WIDTH - 80, SCREEN_HEIGHT - 113) collectionViewLayout:layout];
    //自动翻页
    [self.collection setPagingEnabled:YES];
    
    //写个重用池
    [self.collection registerClass:[LDArtifactItem class] forCellWithReuseIdentifier:@"ArtifactItem"];
    self.collection.backgroundColor = [UIColor whiteColor];
    //签订代理
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    //加到view上
    [self.view addSubview:self.collection];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.collection registerClass:[LDHadelItem class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [layout release];
    [self.collection release];
}


//注册分区标题(section 的 Header)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        LDHadelItem *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        LDArtifactModel *model = [self.array objectAtIndex:indexPath.section];
        headerView.str = model.name;
        
        return headerView;
    }
    return 0;
}



#pragma mark - tab协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LDArtifactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDArtifactCell"];
    LDArtifactModel *model = [self.array objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor redColor];
    if (indexPath.row != 0) {
        UITableViewCell *cellId = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        cellId.textLabel.textColor = [UIColor blackColor];
    }
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:indexPath.row];
    
    //collectionview滑动到第几个SECTION
    [self.collection scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
    
    
}




#pragma mark - collection协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.array.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LDArtifactItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtifactItem" forIndexPath:indexPath];
    LDArtifactModel *model = [[LDArtifactModel alloc]init];
    [model setValuesForKeysWithDictionary:[[self.array[indexPath.section] subcategories] objectAtIndex:indexPath.item]];
    item.model = model;
    
    [model release];
    return item;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [[self.array[section] subcategories] count];
}

//瀑布流点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LDArtifactGiftController *Gift = [[LDArtifactGiftController alloc]init];
    LDArtifactModel *model = [[LDArtifactModel alloc]init];
   [model setValuesForKeysWithDictionary:[[self.array[indexPath.section] subcategories] objectAtIndex:indexPath.item]];
    Gift.num = model.Cid;
    Gift.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:Gift animated:YES];
    
    [Gift release];
    [model release];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    
    [self handleData];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    if ([passWord isEqualToString:@"yes"]) {
        self.tab.backgroundColor = [UIColor blackColor];
        self.collection.backgroundColor = [UIColor blackColor];
    }

}

- (void)viewWillAppear:(BOOL)animated{
    if (self.array.count == 0) {
        [self handleData];
        [self.tab reloadData];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
